/*
 * @Descripttion: 
 * @version: 
 * @Author: Joker
 * @Date: 2021-05-24 18:02:33
 * @LastEditors: Joker
 * @LastEditTime: 2021-07-12 14:42:27
 */
const signAlgo = require("../../util/signAlgo");
const timerS = require("../s/timerS");
const timerV = require("../v/timerV");
const config = require(`${global.rootPath}/config/${process.env.CONFIG}`);

const set = async (uuid, body) => {
    let param = {
        create_time: body.create_time || '', //请求时间
        notify_url: body.notify_url || '', //回调地址
        type: body.type || '', //请求类型 1 单次 2 每天 3 每周 4 每月
        trigger_time: body.trigger_time || '', //触发时间 单次类型为格式化时间，否则为 6个占位符从左到右分别代表：秒、分、时、日、月、周几
        data: body.data || '', //请求数据 json格式
        sign: body.sign || '' //签名
    };
    let v = new timerV(param);
    if (!v.set_check())
        throw new Error(v.msg);
    // console.log(signAlgo.sign(param,config.secret));
    if (!signAlgo.verify(param, config.secret))
        throw new Error("验证签名失败");
    return await timerS.set(uuid, param);
}

const cancel = async (uuid, body) => {
    let param = {
        create_time: body.create_time || '', //请求时间
        id: body.id || '',
        sign: body.sign || '' //签名
    }
    let v = new timerV(param);
    if (!v.cancel_check())
        throw new Error(v.msg);
    // console.log(signAlgo.sign(param, config.secret));
    if (!signAlgo.verify(param, config.secret))
        throw new Error("验证签名失败");
    // return timerS.cancel(uuid,param);
    return await timerS.cancel_msg(uuid, param);
}

module.exports = {
    set: set,
    cancel: cancel
}